import android.util.Base64
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec

object CryptUtils {

    fun encryptAes(text: String, secretKey: String): String {
        try {
            val iv = ByteArray(16) // Initialize the IV (Initialization Vector) for ECB mode
            val ivParameterSpec = IvParameterSpec(iv)
            val secretKeySpec = SecretKeySpec(secretKey.toByteArray(Charsets.UTF_8), "AES")

            val cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec)

            val encrypted = cipher.doFinal(text.toByteArray(Charsets.UTF_8))
            return bytesToHex(encrypted)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ""
    }

    fun bytesToHex(bytes: ByteArray): String {
        val hexArray = "0123456789ABCDEF".toCharArray()
        val hexChars = CharArray(bytes.size * 2)
        for (i in bytes.indices) {
            val v = bytes[i].toInt() and 0xFF
            hexChars[i * 2] = hexArray[v ushr 4]
            hexChars[i * 2 + 1] = hexArray[v and 0x0F]
        }
        return String(hexChars)
    }


    fun decrypt(seed: String, encrypted: String): String? {
        try {
            val rawKey = seed.toByteArray()
            val enc = fromHexToBytes(encrypted)
            val result = decrypt(rawKey, enc)
            return String(result)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    fun encrypt(password: String, cleartext: String): String? {
        try {
            val rawKey = password.toByteArray()
            val result = encrypt(rawKey, cleartext.toByteArray())
            return toHex(result)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    private fun toHex(buf: ByteArray): String {
        val result = StringBuffer(2 * buf.size)
        for (b in buf) {
            appendHex(result, b)
        }
        return result.toString()
    }

    private const val HEX = "0123456789ABCDEF"

    private fun encrypt(raw: ByteArray, clear: ByteArray): ByteArray {
        val skeySpec = SecretKeySpec(raw, "AES")
        val cipher = Cipher.getInstance("AES")
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec)
        return cipher.doFinal(clear)
    }

    private fun decrypt(raw: ByteArray, encrypted: ByteArray): ByteArray {
        val skeySpec = SecretKeySpec(raw, "AES")
        val cipher = Cipher.getInstance("AES")
        cipher.init(Cipher.DECRYPT_MODE, skeySpec)
        return cipher.doFinal(encrypted)
    }

    private fun fromHexToBytes(hexString: String): ByteArray {
        val len = hexString.length / 2
        val result = ByteArray(len)
        for (i in 0 until len) {
            result[i] =
                Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).toByte()
        }
        return result
    }



    private fun appendHex(sb: StringBuffer, b: Byte) {
        sb.append(HEX[b.toInt() shr 4 and 0x0f]).append(HEX[b.toInt() and 0x0f])
    }
}
